<?php
namespace app\api\controller;


use think\Config;
use think\Db;

class Weim extends Base
{
    /**
    *  生成自动登录地址
    *  通过此方法生成的地址，可以让用户免登录，进入积分兑换商城
    */
    public function buildAutoLoginH5(){
        $access_token = Db::table('up_about')->where('about_id',100)->value('about_content');
//        $url = 'https://dopen.weimob.com/api/1_0/janus/config/setConfig?accesstoken=' . $access_token;
//        $body = [
//            'loginCallbackUrl' => 'http://ctruckapp.ctruck.com.cn/api/Weim/loginback'
//        ];
//        $body = json_encode($body);
//        $res = $this->curl_post($url,$body);
//        dump($res);die;
        $url = 'https://dopen.weimob.com/api/1_0/janus/core/login?accesstoken=' . $access_token;
        if(isset($_REQUEST['user_id']) && $_REQUEST['user_id'] > 0){
            $uid = $_REQUEST['user_id'];
            $user = Db::table('up_u_user')->where('user_id', $uid)->field('user_username,user_image,user_phone')->find();
            $body = [
                'openUserId' => $uid,
                'nickname' => $user['user_username'],
                'headurl' => $user['user_image'],
                'telephone' => $user['user_phone'],
            ];
            $body = json_encode($body);
            $res = $this->curl_post($url,$body);
            $appTicket = $res['data']['appTicket'];
            $appurl = 'https://100000888086.retail.n.weimob.com/saas/retail/100000888086/2090309086/goods/list';
            $appbody = "?appTicket=$appTicket&appClientId=6FF84E0A2CD97C7D4118F2C1887B815B";
            return json(['status'=>200,'msg'=>$appurl . $appbody]);
        }else{
            $appurl = 'https://100000888086.retail.n.weimob.com/saas/retail/100000888086/2090309086/goods/list';
            return json(['status'=>200,'msg'=>$appurl]);
        }
    }
    public function curl_post($url , $data=array()){
        $header = array(
            'Accept:application/json',
            'Content-Type:application/json;charset=UTF-8',
        );
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($result,true);
        return $res;
    }
    public function getTwoTokenByCode()
    {
        $url = "https://dopen.weimob.com/fuwu/b/oauth2/token";

        $code = 'Qstlkm';
        $grant_type = 'authorization_code';
        $client_id = '6FF84E0A2CD97C7D4118F2C1887B815B';
        $client_secret = '9C99A7C782BA9BBA04D1F0E1EE5D99E8';
        $redirect_uri = 'https://100000888086.retail.n.weimob.com/saas/retail/100000888086/2090309086/goods/list';

        $body = "$url?code=$code&grant_type=$grant_type&client_id=$client_id&client_secret=$client_secret&redirect_uri=$redirect_uri";

        $res = https_request($body,'','POST');
        $res = json_decode($res,true);
        dump($res);die;
    }
    public function acctokenEveryHour()
    {
        $refresh_token = '563eccb6-21ef-4c96-82ae-f63720e77ff0';
        $grant_type = 'refresh_token';
        $client_id = '6FF84E0A2CD97C7D4118F2C1887B815B';
        $client_secret = '9C99A7C782BA9BBA04D1F0E1EE5D99E8';
        $url = 'https://dopen.weimob.com/fuwu/b/oauth2/token';
        $body = "$url?grant_type=$grant_type&client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token";
        $res = https_request($body,'','POST');
        $res = json_decode($res,true);
        $access_token = $res['access_token'];
        Db::table('up_about')->where('about_id',100)->setField('about_content',$access_token);
    }
    public function loginOutH5()
    {
        $access_token = Db::table('up_about')->where('about_id',100)->value('about_content');
        $url = 'https://dopen.weimob.com/api/1_0/janus/core/logout?accesstoken=' . $access_token;
        $body = [
            'openUserId' => $_POST['user_id']
        ];
        $body = json_encode($body);
        $res = $this->curl_post($url,$body);
        return json(['status'=>200,'msg'=>'ok']);
    }
}